<template>
  <div>
    <el-dialog :title="title" :visible.sync="examineShow" append-to-body>
      <el-form ref="form" :model="form"  label-width="100px">
        <el-form-item label="意见:">
          <el-input type="textarea" style="width: 100%" :autosize="{ minRows: 2, maxRows: 4}" placeholder="请输入意见"
            v-model="form.comment">
          </el-input>
        </el-form-item>
        <!-- <el-form-item label="附件">
          <el-upload class="upload-demo" :file-list="form.fileList" drag
            action="https://jsonplaceholder.typicode.com/posts/" multiple>
            <i class="el-icon-upload" style="color: #C0C4CC;"></i>
            <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
          </el-upload>
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="btnLoading" type="primary" @click="examineSubmit(false)">确 定</el-button>
        <el-button :loading="btnLoading" @click="examineShow=false">取 消</el-button>
      </div>
    </el-dialog>
    <action-button v-if="taskName" :taskName="taskName" :loading="btnLoading"  :showClose="true"
     @changeExamineShow="changeExamineShow" @openClose="openClose"/>
  </div>
</template>

<script>
  import {
    audit
  } from "@/api/resources/apply.js"
    import { uploadFile } from "@/api/system/upload.js";
	import actionButton from "@/components/actionButton/index.vue";
  export default {
    name: "buttons",
    components: {actionButton},
    props: {
      taskId: {
        type: String,
        default () {
          return 0;
        }
      },
      taskName: {
        type: String,
        default () {
          return '';
        }
      },
      detail: {
        type: Object,
        default () {
          return {};
        }
      }
    },
    data() {
      return {
        //按钮loading
        btnLoading: false,
        //弹窗标题
        title: '',
        // 审核弹窗
        examineShow: false,
        //角色列表
        role: [],
        //表单数据
        form: {
          // 意见
          comment: this.title,
          //文件
          fileList: []
        },
      };
    },
    created() {},
    methods: {
      submit() {
        this.$confirm('确定要提交审核吗？', '提示', {
          type: 'warning'
        }).then(
          () => {
            this.form = {
              comment: this.title,
              fileList: []
            }
            this.examineSubmit(4)
          }
        )
      },
      //关闭外层弹窗
      openClose() {
        this.$emit('openClose', false)
      },
      //显示审核弹窗
      changeExamineShow(num, title) {
        this.form = {
          comment: title,
          fileList: []
        }
        this.title = title
        if (num == 4) {
          this.submit()
        }else {
          this.examineShow = num
        }
      },
      //审核提交
      examineSubmit(num) {
        let flag = true
        if (this.title == '派发') {
          this.$refs.form.validate((valid) => {
            flag = valid;
          });
        }
        if (!flag) {
          return
        }

        this.btnLoading = true
        let examineShow = num || this.examineShow
        let variables = {
          actionName_: this.title
        }
        if(this.taskName == '云服务商处理'){
          let supplierFiles = new FormData()
          supplierFiles.append("key", "file");
          this.detail.supplierFiles.forEach((item) => {
              if(item.raw){
                supplierFiles.append("file", item.raw);
              }
          });
          if(supplierFiles.get('file')){
            uploadFile(supplierFiles).then(res=>{
            this.detail.supplierFiles = res.data
            audit({
              taskId: this.taskId,
              comment: this.form.comment,
              actionName: this.title,
              variables,
              action: examineShow != 2 ? 'COMPLETE' : this.taskName == '监管平台接收' ? 'REJECT' : 'BACK',
              formObject: this.detail,
            }).then(
              response => {
                this.$message({
                  message: response.msg,
                  type: 'success'
                })
                this.examineShow = false
                this.$emit('openClose', true)
              }
            ).finally(() => {
              this.btnLoading = false
            })
          })
          return
          }
        }
        audit({
          taskId: this.taskId,
          comment: this.form.comment,
          actionName: this.title,
          variables,
          action: examineShow != 2 ? 'COMPLETE' : this.taskName == '监管平台接收' ? 'REJECT' : 'BACK',
          formObject: this.detail,
        }).then(
          response => {
            this.$message({
              message: response.msg,
              type: 'success'
            })
            this.examineShow = false
            this.$emit('openClose', true)
          }
        ).finally(() => {
          this.btnLoading = false
        })
      },
    }
  };
</script>

<style scoped lang="less">

</style>
